<?php
/**
 * Class Engine created by Havriniuc Serghei<br>
 * Date: 24.07.12<br>
 * Time: 15:26<br>
 * A superclass for all the Model and Controller classes.
 */

class Engine {
	/**
	 * @var \TemplateParser Contains the template object
	 * @see \TemplateParser
	 */
	public static $TPL;
	/**
	 * @var \Log Contains the an instance of the Log class, used for logging
	 */
	public static $LOG;
	/**
	 * @var \MySQL Contains an instance of the MySQL class, used for adressing the DB
	 * @see \MySQL
	 */
	public static $DB;
	/**
	 * @var \Session A variable for handling the session
	 */
	public static $SESSION;
	/**
	 * @var \Page
	 */
	public static $CONTROLLER;
	/**
	 * @var \Debug
	 */
	public static $DEBUG;
	/**
	 * @var string containing the message for the user in case there is one
	 */
	public static $MESSAGE;
	/**
	 * @var array containing the full info regarding current page
	 */
	public static $PAGE;
	/**
	 * @var string the action. Used for detecting the method from controller.
	 */
	public static $ACTION;
	/**
	 * @var array with global_variables content values
	 */
	public static $SETTINGS;
	/**
	 * @var integer used for pagination (current page)
	 */
	public static $P;
	/**
	 * @var array where will be stocked user data
	 */
	public static $USER;
	/**
	* @var  PHPMailer
	*/
	public static $MAIL;

	/**
	 * A function called automatically from initialize.php. Initializes all the important global objects, classes and
	 * functionality like the cache, the default configurations, the template parser, the debug class, the error handler
	 * the logger class, the DB connector class, the session class, the user login class, the current project's settings,
	 * it also starts the request's execution time counter.
	 * This method behaves differently depending on whether or not the CLI constant is defined. CLI stands for 'command
	 * line interface' and, normally, if it's defined, it means the engine is loaded via command line to execute some
	 * special action, like a cron job. Otherwise, if it's not defined, it means the engine load was triggered by a
	 * browser. For example, Cache, Session and User Login functionality initializes only if the engine was loaded from
	 * a browser.
	 * This method relies on the Db_Cms_Pages::getCurrentPage() method to find out what the current page is, that is, what
	 * page corresponds to the URL the user had accessed or to the parameter passed in the command line. If successful,
	 * an instance of that page's controller will be saved in the $CONTROLLER field of this class, otherwise, a 404 error
	 * page will be issued and shown to user.
	 * This method also registers a shutdown function, see onBeforeShutdown()
	 * @see \Page
	 * @see \CLI
	 * @see \Cache
	 * @see \Config
	 * @see \TemplateParser
	 * @see \Debug
	 * @see \Log
	 * @see \MySQL
	 * @see \Db_Projects
	 * @see \Session
	 * @see \Login
	 * @see \Db_Cms_Settings
	 * @see \onBeforeShutdown()
	 * @see \errorHandler($errorNumber,$errorString,$errorFile,$errorLine)
	 * @see register_shutdown_function($callback)
	 * @see set_error_handler($callback,$level)
	 * @uses \Db_Cms_Pages::getCurrentPage()
	 */
    public static function boot() {


		Utils::startTimer();
		Config::init();
		date_default_timezone_set(conf('DEFAULT_TIMEZONE'));


        set_error_handler('errorHandler', E_ALL | E_STRICT);
        register_shutdown_function('onBeforeShutdown');


        /**
         *  Initializing the Template Parser object.
         */
		self::$TPL = new TemplateParser();


        /**
         *  Initializing the Debug object.
         */
		self::$DEBUG = new Debug();


		/**
		 * Initializing the log object.
		 * Usage: $LOG->write($contentToWrite, 'filename');
		 * The "filename.LOGS_EXT" will be created/updated in LOGS_PATH
		 */
		self::$LOG = new Log();

        /**
         *  Initializing the Mysql object.
         */
		self::$DB = new MySQL();


		Db_Projects::setCurrentProject();

		/** 
		 * Initiate the Session class only in case we're running in a browser and not in a Command Line Interface (CLI)
		 */
			self::$S = new Session();
			self::$MESSAGE = self::$S->message();
			self::$P = get('p', 'int');

			//check if login
			if (self::$PROJECT['pr_needs_login']) {
				Login::isLoggedIn();
			}

		Config::loadConfigFromDb();

		self::$SETTINGS = Db_Cms_Settings::getSettings();

		/**
		 * Here we start to load the controller:
		 *  - page content in case we're loading the page from a browser
		 *  - simply execute the script in case the CLI is defined
		 */
		Db_Cms_Pages::getCurrentPage();

		if (class_exists(self::$PAGE['p_controller'])) {
			self::$CONTROLLER = new self::$PAGE['p_controller'];
		} elseif (false) {
			/**
			 * @todo Here we will put the logic for pages with dynamic URL
			 */
		} else {
			Db_Cms_Pages::get404Page();
			self::$CONTROLLER = new self::$PAGE['p_controller'];
		}
		/**
		 * End of Controllers loading.
		 */
	}
}
